﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ThreadDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(new TicketSeller().Sell);
            t1.Name = "1号台";
            t1.IsBackground = true;

            Thread t2 = new Thread(new TicketSeller().Sell);
            t2.Name = "2号台";
            t2.IsBackground = true;

            t1.Start();
            t2.Start();
            Console.ReadLine();
        }
    }
    public class TicketSeller
    {
        public static int count = 100;
        public static object locker = new object();
        public void Sell()
        {
           
                while (count > 0)
                {
                lock (locker)
                {
                    TicketSeller.count--;
                    Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ",还剩：" + count + "张");
                }
            }
        }    
        #region 2.0使用moonitor
        public void SellByMonitor()
        {
            //使用monitor此处会经常抛出异常，必须加try
            bool isGetLocker=false;
            try
            {
                while (count > 0)
                {
                    Monitor.Enter(locker,ref isGetLocker);//尝试获取锁，如果获取成功，isGetLocker=true,否则=false
                    Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ",还剩：" + count + "张");
                   
                }
            }
            catch (Exception ex)
            {

                throw  ex;
            }
            finally
            {
                //如果有锁就解锁，否则没有锁就补充在解锁
                if (isGetLocker)
                {
                    Monitor.Exit(locker);
                }
            }
        } 
        #endregion
    }
    /// <summary>
    /// 产品
    /// </summary>
    public class Dog
    {
        public static object lockCommunicate;
        public string name;
        public int age;
    }
    /// <summary>
    /// 生产者
    /// </summary>
    public class Productor
    {
        Dog myDog;
        public Productor(Dog d)
        {
            myDog = d;
        }
        bool type = true;
        public void ProductDog()
        {
            lock (Dog.lockCommunicate)
            {
                while (true)
                {

                    if (type)
                    {
                        myDog.name = "Rulky";
                        myDog.age = 2;
                    }
                    else
                    {
                        myDog.name = "Rose";
                        myDog.age = 1;
                    }
                    type = !type;
                    Monitor.Wait(Dog.lockCommunicate);
                    Monitor.Pulse(Dog.lockCommunicate);
                }
            }     
        }
    }
    public class Customer
    {
        Dog myDog;
        public Customer(Dog d)
        {
            myDog = d;
        }
        public void ShowDogInfo()
        {
            lock (Dog.lockCommunicate)
            {
                while (true)
                {
                   
                    Console.WriteLine("我是{0}，年龄{1}", myDog.name, myDog.age);
                    Monitor.Pulse(Dog.lockCommunicate);
                    Monitor.Wait(Dog.lockCommunicate);
                }
            }       
        }
        public void TestProCus()
        {
            Dog d = new Dog();
            Productor p = new Productor(d);
            Customer c = new Customer(d);
            Thread trhPro = new Thread(p.ProductDog);
            trhPro.IsBackground = true;
            trhPro.Name = "t1";

            Thread trhCus = new Thread(c.ShowDogInfo);
            trhCus.IsBackground = true;
            trhCus.Name = "t2";

            trhPro.Start();
            trhCus.Start();
        }
    }
  
    /// <summary>
    /// 
    /// </summary>
    public class TicketSeller1
    {

    }
}
